package cn.tedu.sp02.item.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {
    /**
     *
     *测试负载均衡是, 在浏览器显示端口号
     * */
    @Value("${server.port}")
    private  int port;

    @Autowired
    private ItemService itemService;

    @GetMapping("/{orderId}")   //和下面等价
    //RequestMapping(path="/{orderId}",method=RequestMethod.GET)   // GET

    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        log.info("获取订单的商品列表:orderId="+orderId);

        //随机的延迟
        if(Math.random()<0.9){
            //延迟
            long t=new Random().nextInt(5000);
            log.info("延迟:"+t);
            Thread.sleep(t);

        }




        List<Item> items=itemService.getItems(orderId);
        return  JsonResult.ok().msg("port="+port).data(items);

    }
    /**
     *
     * @RequestBody 完整接收请求协议体的类容,转成商品集合
     * */
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items) {
        itemService.decreaseNumber(items);
        return JsonResult.ok().msg("减少商品库存成功");
    }


}
